package com.qk.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.PageResult;
import com.qk.dto.ActivityDto;
import com.qk.entity.Activity;
import com.qk.mapper.ActivityMapper;
import com.qk.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: ActivityServiceImpl
 * Package: com.qk.service.impl
 * Description:
 *
 * @Author zt
 * @Create 2025/7/30 20:59
 * @Version 1.0
 */
@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    /*
    1.活动列表的分页查询
     */
    @Override
    public PageResult<Activity> listActivities(ActivityDto activityDto) {
        //1.设置分页参数
        PageHelper.startPage(activityDto.getPage(), activityDto.getPageSize());

        //2.调用 mapper接口查询
        List<Activity> activityList = activityMapper.selectByCondition(activityDto);

        //构建分页查询之后的结果
       //强转，Page<Activity> p = (Page<Activity>) activityList;
        PageInfo<Activity> pageInfo = new PageInfo<>(activityList);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /*
    2.删除活动
     */
    @Override
    public void delete(Integer id) {
        activityMapper.delete(id);
    }

    /*
    3.添加活动
     */
    @Override
    public void add(Activity activity) {
        //补充基础属性：添加时间和更新时间
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.add(activity);
    }

    /*
    4.根据ID查询
     */
    @Override
    public Activity getById(Integer id) {
        return activityMapper.getById(id);
    }

    /*
    5.修改活动
     */

    @Override
    public void update(Activity activity) {
        //补充基础属性:更新时间
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.update(activity);
    }

    /*
    6.查询指定的类型活动
     */
    @Override
    public List<Activity> listByType(Integer type) {
        return activityMapper.listByType(type);
    }

    /*
    7.查询所有活动
     */

    @Override
    public List<Activity> list() {
        return activityMapper.list();
    }
}
